
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Computing Derived Data · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ImplementingUndoHistory.html" />
    
    
    <link rel="prev" href="WritingTests.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Read Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../introduction/">
            
                <a href="../introduction/">
            
                    
                    Introduction
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../introduction/Motivation.html">
            
                <a href="../introduction/Motivation.html">
            
                    
                    Motivation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../introduction/CoreConcepts.html">
            
                <a href="../introduction/CoreConcepts.html">
            
                    
                    Core Concepts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../introduction/ThreePrinciples.html">
            
                <a href="../introduction/ThreePrinciples.html">
            
                    
                    Three Principles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../introduction/PriorArt.html">
            
                <a href="../introduction/PriorArt.html">
            
                    
                    Prior Art
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../introduction/Ecosystem.html">
            
                <a href="../introduction/Ecosystem.html">
            
                    
                    Ecosystem
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../introduction/Examples.html">
            
                <a href="../introduction/Examples.html">
            
                    
                    Examples
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/">
            
                <a href="../basics/">
            
                    
                    Basics
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../basics/Actions.html">
            
                <a href="../basics/Actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../basics/Reducers.html">
            
                <a href="../basics/Reducers.html">
            
                    
                    Reducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../basics/Store.html">
            
                <a href="../basics/Store.html">
            
                    
                    Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../basics/DataFlow.html">
            
                <a href="../basics/DataFlow.html">
            
                    
                    Data Flow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../basics/UsageWithReact.html">
            
                <a href="../basics/UsageWithReact.html">
            
                    
                    Usage with React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../basics/ExampleTodoList.html">
            
                <a href="../basics/ExampleTodoList.html">
            
                    
                    Example: Todo List
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../advanced/">
            
                <a href="../advanced/">
            
                    
                    Advanced
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../advanced/AsyncActions.html">
            
                <a href="../advanced/AsyncActions.html">
            
                    
                    Async Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../advanced/AsyncFlow.html">
            
                <a href="../advanced/AsyncFlow.html">
            
                    
                    Async Flow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../advanced/Middleware.html">
            
                <a href="../advanced/Middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../advanced/UsageWithReactRouter.html">
            
                <a href="../advanced/UsageWithReactRouter.html">
            
                    
                    Usage with React Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../advanced/ExampleRedditAPI.html">
            
                <a href="../advanced/ExampleRedditAPI.html">
            
                    
                    Example: Reddit API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" >
            
                <span>
            
                    
                    Next Steps
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    Recipes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="MigratingToRedux.html">
            
                <a href="MigratingToRedux.html">
            
                    
                    Migrating to Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="UsingObjectSpreadOperator.html">
            
                <a href="UsingObjectSpreadOperator.html">
            
                    
                    Using Object Spread Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="ReducingBoilerplate.html">
            
                <a href="ReducingBoilerplate.html">
            
                    
                    Reducing Boilerplate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="ServerRendering.html">
            
                <a href="ServerRendering.html">
            
                    
                    Server Rendering
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="WritingTests.html">
            
                <a href="WritingTests.html">
            
                    
                    Writing Tests
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.6" data-path="ComputingDerivedData.html">
            
                <a href="ComputingDerivedData.html">
            
                    
                    Computing Derived Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="ImplementingUndoHistory.html">
            
                <a href="ImplementingUndoHistory.html">
            
                    
                    Implementing Undo History
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="IsolatingSubapps.html">
            
                <a href="IsolatingSubapps.html">
            
                    
                    Isolating Subapps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="StructuringReducers.html">
            
                <a href="StructuringReducers.html">
            
                    
                    Structuring Reducers
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.9.1" data-path="reducers/PrerequisiteConcepts.html">
            
                <a href="reducers/PrerequisiteConcepts.html">
            
                    
                    Prerequisite Concepts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.2" data-path="reducers/BasicReducerStructure.html">
            
                <a href="reducers/BasicReducerStructure.html">
            
                    
                    Basic Reducer Structure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.3" data-path="reducers/SplittingReducerLogic.html">
            
                <a href="reducers/SplittingReducerLogic.html">
            
                    
                    Splitting Reducer Logic
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.4" data-path="reducers/RefactoringReducersExample.html">
            
                <a href="reducers/RefactoringReducersExample.html">
            
                    
                    Refactoring Reducers Example
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.5" data-path="reducers/UsingCombineReducers.html">
            
                <a href="reducers/UsingCombineReducers.html">
            
                    
                    Using combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.6" data-path="reducers/BeyondCombineReducers.html">
            
                <a href="reducers/BeyondCombineReducers.html">
            
                    
                    Beyond combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.7" data-path="reducers/NormalizingStateShape.html">
            
                <a href="reducers/NormalizingStateShape.html">
            
                    
                    Normalizing State Shape
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.8" data-path="reducers/UpdatingNormalizedData.html">
            
                <a href="reducers/UpdatingNormalizedData.html">
            
                    
                    Updating Normalized Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.9" data-path="reducers/ReusingReducerLogic.html">
            
                <a href="reducers/ReusingReducerLogic.html">
            
                    
                    Reusing Reducer Logic
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.10" data-path="reducers/ImmutableUpdatePatterns.html">
            
                <a href="reducers/ImmutableUpdatePatterns.html">
            
                    
                    Immutable Update Patterns
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9.11" data-path="reducers/InitializingState.html">
            
                <a href="reducers/InitializingState.html">
            
                    
                    Initializing State
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="UsingImmutableJS.html">
            
                <a href="UsingImmutableJS.html">
            
                    
                    Using Immutable.JS with Redux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../FAQ.html">
            
                <a href="../FAQ.html">
            
                    
                    FAQ
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../faq/General.html">
            
                <a href="../faq/General.html">
            
                    
                    General
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../faq/Reducers.html">
            
                <a href="../faq/Reducers.html">
            
                    
                    Reducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../faq/OrganizingState.html">
            
                <a href="../faq/OrganizingState.html">
            
                    
                    Organizing State
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../faq/StoreSetup.html">
            
                <a href="../faq/StoreSetup.html">
            
                    
                    Store Setup
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../faq/Actions.html">
            
                <a href="../faq/Actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../faq/ImmutableData.html">
            
                <a href="../faq/ImmutableData.html">
            
                    
                    Immutable Data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../faq/CodeStructure.html">
            
                <a href="../faq/CodeStructure.html">
            
                    
                    Code Structure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../faq/Performance.html">
            
                <a href="../faq/Performance.html">
            
                    
                    Performance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../faq/ReactRedux.html">
            
                <a href="../faq/ReactRedux.html">
            
                    
                    React Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../faq/Miscellaneous.html">
            
                <a href="../faq/Miscellaneous.html">
            
                    
                    Miscellaneous
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../Troubleshooting.html">
            
                <a href="../Troubleshooting.html">
            
                    
                    Troubleshooting
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../Glossary.html">
            
                <a href="../Glossary.html">
            
                    
                    Glossary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../api/">
            
                <a href="../api/">
            
                    
                    API Reference
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../api/createStore.html">
            
                <a href="../api/createStore.html">
            
                    
                    createStore
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../api/Store.html">
            
                <a href="../api/Store.html">
            
                    
                    Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../api/combineReducers.html">
            
                <a href="../api/combineReducers.html">
            
                    
                    combineReducers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../api/applyMiddleware.html">
            
                <a href="../api/applyMiddleware.html">
            
                    
                    applyMiddleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../api/bindActionCreators.html">
            
                <a href="../api/bindActionCreators.html">
            
                    
                    bindActionCreators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../api/compose.html">
            
                <a href="../api/compose.html">
            
                    
                    compose
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../CHANGELOG.md">
            
                <span>
            
                    
                    Change Log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../PATRONS.md">
            
                <span>
            
                    
                    Patrons
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../Feedback.html">
            
                <a href="../Feedback.html">
            
                    
                    Feedback
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Computing Derived Data</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="computing-derived-data">Computing Derived Data</h1>
<p><a href="https://github.com/reactjs/reselect" target="_blank">Reselect</a> is a simple library for creating memoized, composable <strong>selector</strong> functions. Reselect selectors can be used to efficiently compute derived data from the Redux <a href="../Glossary.html#store" class="glossary-term" title="">store</a>.</p>
<h3 id="motivation-for-memoized-selectors">Motivation for Memoized Selectors</h3>
<p>Let&apos;s revisit the <a href="../basics/UsageWithReact.html">Todos List example</a>:</p>
<h4 id="containersvisibletodolistjs"><code>containers/VisibleTodoList.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> { toggleTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions&apos;</span>
<span class="hljs-keyword">import</span> TodoList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/TodoList&apos;</span>

<span class="hljs-keyword">const</span> getVisibleTodos = (todos, filter) =&gt; {
  <span class="hljs-keyword">switch</span> (filter) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ALL&apos;</span>:
      <span class="hljs-keyword">return</span> todos
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_COMPLETED&apos;</span>:
      <span class="hljs-keyword">return</span> todos.filter(t =&gt; t.completed)
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ACTIVE&apos;</span>:
      <span class="hljs-keyword">return</span> todos.filter(t =&gt; !t.completed)
  }
}

<span class="hljs-keyword">const</span> mapStateToProps = (state) =&gt; {
  <span class="hljs-keyword">return</span> {
    todos: getVisibleTodos(state.todos, state.visibilityFilter)
  }
}

<span class="hljs-keyword">const</span> mapDispatchToProps = (dispatch) =&gt; {
  <span class="hljs-keyword">return</span> {
    onTodoClick: (id) =&gt; {
      dispatch(toggleTodo(id))
    }
  }
}

<span class="hljs-keyword">const</span> VisibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoList)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> VisibleTodoList
</code></pre>
<p>In the above example, <code>mapStateToProps</code> calls <code>getVisibleTodos</code> to calculate <code>todos</code>. This works great, but there is a drawback: <code>todos</code> is calculated every time the component is updated. If the <a href="../Glossary.html#state" class="glossary-term" title="">state</a> tree is large, or the calculation expensive, repeating the calculation on every update may cause performance problems. Reselect can help to avoid these unnecessary recalculations.</p>
<h3 id="creating-a-memoized-selector">Creating a Memoized Selector</h3>
<p>We would like to replace <code>getVisibleTodos</code> with a memoized selector that recalculates <code>todos</code> when the value of <code>state.todos</code> or <code>state.visibilityFilter</code> changes, but not when changes occur in other (unrelated) parts of the <a href="../Glossary.html#state" class="glossary-term" title="">state</a> tree.</p>
<p>Reselect provides a function <code>createSelector</code> for creating memoized selectors. <code>createSelector</code> takes an array of input-selectors and a transform function as its arguments. If the Redux <a href="../Glossary.html#state" class="glossary-term" title="">state</a> tree is mutated in a way that causes the value of an input-selector to change, the selector will call its transform function with the values of the input-selectors as arguments and return the result. If the values of the input-selectors are the same as the previous call to the selector, it will return the previously computed value instead of calling the transform function.</p>
<p>Let&apos;s define a memoized selector named <code>getVisibleTodos</code> to replace the non-memoized version above:</p>
<h4 id="selectorsindexjs"><code>selectors/index.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createSelector } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;reselect&apos;</span>

<span class="hljs-keyword">const</span> getVisibilityFilter = (state) =&gt; state.visibilityFilter
<span class="hljs-keyword">const</span> getTodos = (state) =&gt; state.todos

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> getVisibleTodos = createSelector(
  [ getVisibilityFilter, getTodos ],
  (visibilityFilter, todos) =&gt; {
    <span class="hljs-keyword">switch</span> (visibilityFilter) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ALL&apos;</span>:
        <span class="hljs-keyword">return</span> todos
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_COMPLETED&apos;</span>:
        <span class="hljs-keyword">return</span> todos.filter(t =&gt; t.completed)
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ACTIVE&apos;</span>:
        <span class="hljs-keyword">return</span> todos.filter(t =&gt; !t.completed)
    }
  }
)
</code></pre>
<p>In the example above, <code>getVisibilityFilter</code> and <code>getTodos</code> are input-selectors. They are created as ordinary non-memoized selector functions because they do not transform the data they select. <code>getVisibleTodos</code> on the other hand is a memoized selector. It takes <code>getVisibilityFilter</code> and <code>getTodos</code> as input-selectors, and a transform function that calculates the filtered todos list.</p>
<h3 id="composing-selectors">Composing Selectors</h3>
<p>A memoized selector can itself be an input-selector to another memoized selector. Here is <code>getVisibleTodos</code> being used as an input-selector to a selector that further filters the todos by keyword:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> getKeyword = (state) =&gt; state.keyword

<span class="hljs-keyword">const</span> getVisibleTodosFilteredByKeyword = createSelector(
  [ getVisibleTodos, getKeyword ],
  (visibleTodos, keyword) =&gt; visibleTodos.filter(
    todo =&gt; todo.text.indexOf(keyword) &gt; <span class="hljs-number">-1</span>
  )
)
</code></pre>
<h3 id="connecting-a-selector-to-the-redux-store">Connecting a Selector to the Redux Store</h3>
<p>If you are using <a href="https://github.com/reactjs/react-redux" target="_blank">React Redux</a>, you can call selectors as regular functions inside <code>mapStateToProps()</code>:</p>
<h4 id="containersvisibletodolistjs"><code>containers/VisibleTodoList.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> { toggleTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions&apos;</span>
<span class="hljs-keyword">import</span> TodoList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/TodoList&apos;</span>
<span class="hljs-keyword">import</span> { getVisibleTodos } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../selectors&apos;</span>

<span class="hljs-keyword">const</span> mapStateToProps = (state) =&gt; {
  <span class="hljs-keyword">return</span> {
    todos: getVisibleTodos(state)
  }
}

<span class="hljs-keyword">const</span> mapDispatchToProps = (dispatch) =&gt; {
  <span class="hljs-keyword">return</span> {
    onTodoClick: (id) =&gt; {
      dispatch(toggleTodo(id))
    }
  }
}

<span class="hljs-keyword">const</span> VisibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoList)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> VisibleTodoList
</code></pre>
<h3 id="accessing-react-props-in-selectors">Accessing React Props in Selectors</h3>
<blockquote>
<p>This section introduces a hypothetical extension to our app that allows it to support multiple Todo Lists. Please note that a full implementation of this extension requires changes to the reducers, components, actions etc. that aren&apos;t directly relevant to the topics discussed and have been omitted for brevity.</p>
</blockquote>
<p>So far we have only seen selectors receive the Redux <a href="../Glossary.html#store" class="glossary-term" title="">store</a> <a href="../Glossary.html#state" class="glossary-term" title="">state</a> as an argument, but a selector can receive props too.</p>
<p>Here is an <code>App</code> component that renders three <code>VisibleTodoList</code> components, each of which has a <code>listId</code> prop:</p>
<h4 id="componentsappjs"><code>components/App.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> Footer <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./Footer&apos;</span>
<span class="hljs-keyword">import</span> AddTodo <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/AddTodo&apos;</span>
<span class="hljs-keyword">import</span> VisibleTodoList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../containers/VisibleTodoList&apos;</span>

<span class="hljs-keyword">const</span> App = () =&gt; (
  &lt;div&gt;
    &lt;VisibleTodoList listId=&quot;1&quot; /&gt;
    &lt;VisibleTodoList listId=&quot;2&quot; /&gt;
    &lt;VisibleTodoList listId=&quot;3&quot; /&gt;
  &lt;/div&gt;
)
</code></pre>
<p>Each <code>VisibleTodoList</code> container should select a different slice of the <a href="../Glossary.html#state" class="glossary-term" title="">state</a> depending on the value of the <code>listId</code> prop, so let&apos;s modify <code>getVisibilityFilter</code> and <code>getTodos</code> to accept a props argument:</p>
<h4 id="selectorstodoselectorsjs"><code>selectors/todoSelectors.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createSelector } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;reselect&apos;</span>

<span class="hljs-keyword">const</span> getVisibilityFilter = (state, props) =&gt;
  state.todoLists[props.listId].visibilityFilter

<span class="hljs-keyword">const</span> getTodos = (state, props) =&gt;
  state.todoLists[props.listId].todos

<span class="hljs-keyword">const</span> getVisibleTodos = createSelector(
  [ getVisibilityFilter, getTodos ],
  (visibilityFilter, todos) =&gt; {
    <span class="hljs-keyword">switch</span> (visibilityFilter) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_COMPLETED&apos;</span>:
        <span class="hljs-keyword">return</span> todos.filter(todo =&gt; todo.completed)
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ACTIVE&apos;</span>:
        <span class="hljs-keyword">return</span> todos.filter(todo =&gt; !todo.completed)
      <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> todos
    }
  }
)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> getVisibleTodos
</code></pre>
<p><code>props</code> can be passed to <code>getVisibleTodos</code> from <code>mapStateToProps</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> mapStateToProps = (state, props) =&gt; {
  <span class="hljs-keyword">return</span> {
    todos: getVisibleTodos(state, props)
  }
}
</code></pre>
<p>So now <code>getVisibleTodos</code> has access to <code>props</code>, and everything seems to be working fine.</p>
<p><strong>But there is a problem!</strong></p>
<p>Using the <code>getVisibleTodos</code> selector with multiple instances of the <code>visibleTodoList</code> container will not correctly memoize:</p>
<h4 id="containersvisibletodolistjs"><code>containers/VisibleTodoList.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> { toggleTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions&apos;</span>
<span class="hljs-keyword">import</span> TodoList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/TodoList&apos;</span>
<span class="hljs-keyword">import</span> { getVisibleTodos } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../selectors&apos;</span>

<span class="hljs-keyword">const</span> mapStateToProps = (state, props) =&gt; {
  <span class="hljs-keyword">return</span> {
    <span class="hljs-comment">// WARNING: THE FOLLOWING SELECTOR DOES NOT CORRECTLY MEMOIZE</span>
    todos: getVisibleTodos(state, props)
  }
}

<span class="hljs-keyword">const</span> mapDispatchToProps = (dispatch) =&gt; {
  <span class="hljs-keyword">return</span> {
    onTodoClick: (id) =&gt; {
      dispatch(toggleTodo(id))
    }
  }
}

<span class="hljs-keyword">const</span> VisibleTodoList = connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoList)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> VisibleTodoList
</code></pre>
<p>A selector created with <code>createSelector</code> only returns the cached value when its set of arguments is the same as its previous set of arguments. If we alternate between rendering <code>&lt;VisibleTodoList listId=&quot;1&quot; /&gt;</code> and <code>&lt;VisibleTodoList listId=&quot;2&quot; /&gt;</code>, the shared selector will alternate between receiving <code>{listId: 1}</code> and <code>{listId: 2}</code> as its <code>props</code> argument. This will cause the arguments to be different on each call, so the selector will always recompute instead of returning the cached value. We&apos;ll see how to overcome this limitation in the next section.</p>
<h3 id="sharing-selectors-across-multiple-components">Sharing Selectors Across Multiple Components</h3>
<blockquote>
<p>The examples in this section require React Redux v4.3.0 or greater</p>
</blockquote>
<p>In order to share a selector across multiple <code>VisibleTodoList</code> components <strong>and</strong> retain memoization, each instance of the component needs its own private copy of the selector.</p>
<p>Let&apos;s create a function named <code>makeGetVisibleTodos</code> that returns a new copy of the <code>getVisibleTodos</code> selector each time it is called:</p>
<h4 id="selectorstodoselectorsjs"><code>selectors/todoSelectors.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createSelector } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;reselect&apos;</span>

<span class="hljs-keyword">const</span> getVisibilityFilter = (state, props) =&gt;
  state.todoLists[props.listId].visibilityFilter

<span class="hljs-keyword">const</span> getTodos = (state, props) =&gt;
  state.todoLists[props.listId].todos

<span class="hljs-keyword">const</span> makeGetVisibleTodos = () =&gt; {
  <span class="hljs-keyword">return</span> createSelector(
    [ getVisibilityFilter, getTodos ],
    (visibilityFilter, todos) =&gt; {
      <span class="hljs-keyword">switch</span> (visibilityFilter) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_COMPLETED&apos;</span>:
          <span class="hljs-keyword">return</span> todos.filter(todo =&gt; todo.completed)
        <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;SHOW_ACTIVE&apos;</span>:
          <span class="hljs-keyword">return</span> todos.filter(todo =&gt; !todo.completed)
        <span class="hljs-keyword">default</span>:
          <span class="hljs-keyword">return</span> todos
      }
    }
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> makeGetVisibleTodos
</code></pre>
<p>We also need a way to give each instance of a container access to its own private selector. The <code>mapStateToProps</code> argument of <code>connect</code> can help with this.</p>
<p><strong>If the <code>mapStateToProps</code> argument supplied to <code>connect</code> returns a function instead of an object, it will be used to create an individual <code>mapStateToProps</code> function for each instance of the container.</strong></p>
<p>In the example below <code>makeMapStateToProps</code> creates a new <code>getVisibleTodos</code> selector, and returns a <code>mapStateToProps</code> function that has exclusive access to the new selector:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> makeMapStateToProps = () =&gt; {
  <span class="hljs-keyword">const</span> getVisibleTodos = makeGetVisibleTodos()
  <span class="hljs-keyword">const</span> mapStateToProps = (state, props) =&gt; {
    <span class="hljs-keyword">return</span> {
      todos: getVisibleTodos(state, props)
    }
  }
  <span class="hljs-keyword">return</span> mapStateToProps
}
</code></pre>
<p>If we pass <code>makeMapStateToProps</code> to <code>connect</code>, each instance of the <code>VisibleTodosList</code> container will get its own <code>mapStateToProps</code> function with a private <code>getVisibleTodos</code> selector. Memoization will now work correctly regardless of the render order of the <code>VisibleTodoList</code> containers.</p>
<h4 id="containersvisibletodolistjs"><code>containers/VisibleTodoList.js</code></h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> { toggleTodo } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions&apos;</span>
<span class="hljs-keyword">import</span> TodoList <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/TodoList&apos;</span>
<span class="hljs-keyword">import</span> { makeGetVisibleTodos } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../selectors&apos;</span>

<span class="hljs-keyword">const</span> makeMapStateToProps = () =&gt; {
  <span class="hljs-keyword">const</span> getVisibleTodos = makeGetVisibleTodos()
  <span class="hljs-keyword">const</span> mapStateToProps = (state, props) =&gt; {
    <span class="hljs-keyword">return</span> {
      todos: getVisibleTodos(state, props)
    }
  }
  <span class="hljs-keyword">return</span> mapStateToProps
}

<span class="hljs-keyword">const</span> mapDispatchToProps = (dispatch) =&gt; {
  <span class="hljs-keyword">return</span> {
    onTodoClick: (id) =&gt; {
      dispatch(toggleTodo(id))
    }
  }
}

<span class="hljs-keyword">const</span> VisibleTodoList = connect(
  makeMapStateToProps,
  mapDispatchToProps
)(TodoList)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> VisibleTodoList
</code></pre>
<h2 id="next-steps">Next Steps</h2>
<p>Check out the <a href="https://github.com/reactjs/reselect" target="_blank">official documentation</a> of Reselect as well as its <a href="https://github.com/reactjs/reselect#faq" target="_blank">FAQ</a>. Most Redux projects start using Reselect when they have performance problems because of too many derived computations and wasted re-renders, so make sure you are familiar with it before you build something big. It can also be useful to study <a href="https://github.com/reactjs/reselect/blob/master/src/index.js" target="_blank">its source code</a> so you don&apos;t think it&apos;s magic.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="WritingTests.html" class="navigation navigation-prev " aria-label="Previous page: Writing Tests">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="ImplementingUndoHistory.html" class="navigation navigation-next " aria-label="Next page: Implementing Undo History">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Computing Derived Data","level":"1.5.6","depth":2,"next":{"title":"Implementing Undo History","level":"1.5.7","depth":2,"path":"recipes/ImplementingUndoHistory.md","ref":"./recipes/ImplementingUndoHistory.md","articles":[]},"previous":{"title":"Writing Tests","level":"1.5.5","depth":2,"path":"recipes/WritingTests.md","ref":"./recipes/WritingTests.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"recipes/ComputingDerivedData.md","mtime":"2017-03-13T14:28:11.335Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-15T14:47:19.389Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

